package users

import (
	"github.com/gin-gonic/gin"
	"wskyygyso/api/app/models"
	"wskyygyso/api/app/models/user"
	"wskyygyso/api/app/requests"
	"wskyygyso/api/pkg/auth"
	"wskyygyso/api/pkg/config"
	"wskyygyso/api/pkg/file"
	"wskyygyso/api/pkg/response"
)

type UsersController struct {
	models.BaseModel
}

func (ctrl *UsersController) Index(c *gin.Context) {
	var request = requests.PaginationRequest{}
	if ok := requests.Validate(c, &request, requests.Pagination); !ok {
		return
	}
	users, paging := user.Paginate(c, 10)
	response.JSON(c, gin.H{
		"data":  users,
		"pager": paging,
	})
}

func (ctrl *UsersController) Show(c *gin.Context) {
	userModel := user.Get(c.Param("id"))
	if userModel.ID == 0 {
		response.Abort404(c)
		return
	}
	response.Data(c, userModel)
}

func (ctrl *UsersController) Store(c *gin.Context) {

	request := requests.UserRequest{}
	if ok := requests.Validate(c, &request, requests.UserSave); !ok {
		return
	}

	userModel := user.User{
		//  FieldName:      request.FieldName,
	}
	userModel.Create()
	if userModel.ID > 0 {
		response.Created(c, userModel)
	} else {
		response.Abort500(c, "创建失败，请稍后尝试~")
	}
}

func (ctrl *UsersController) Update(c *gin.Context) {

	userModel := user.Get(c.Param("id"))
	if userModel.ID == 0 {
		response.Abort404(c)
		return
	}

	//  if ok := policies.CanModifyUser(c, userModel); !ok {
	//      response.Abort403(c)
	//      return
	//  }

	request := requests.UserRequest{}
	bindOk := requests.Validate(c, &request, requests.UserSave)
	if !bindOk {
		return
	}

	//userModel.FieldName = request.FieldName
	rowsAffected := userModel.Save()
	if rowsAffected > 0 {
		response.Data(c, userModel)
	} else {
		response.Abort500(c, "更新失败，请稍后尝试~")
	}
}

func (ctrl *UsersController) Delete(c *gin.Context) {

	userModel := user.Get(c.Param("id"))
	if userModel.ID == 0 {
		response.Abort404(c)
		return
	}

	// if ok := policies.CanModifyUser(c, userModel); !ok {
	//     response.Abort403(c)
	//     return
	// }

	rowsAffected := userModel.Delete()
	if rowsAffected > 0 {
		response.SUCCESS(c)
		return
	}

	response.Abort500(c, "删除失败，请稍后尝试~")
}

func (ctrl *UsersController) CurrentUser(c *gin.Context) {
	model := auth.CurrentUser(c)
	response.Data(
		c,
		model)
}

func (ctrl *UsersController) UpdateProfile(c *gin.Context) {
	request := requests.UserUpdateProfileRequest{}
	if ok := requests.Validate(c, &request, requests.UserUpdateSave); !ok {
		return
	}
	user := auth.CurrentUser(c)
	user.Name = request.Name
	user.City = request.City
	user.Introduction = request.Introduction
	rows := user.Save()
	if rows > 0 {
		response.Data(c, user)
	} else {
		response.Abort500(c, "请稍后尝试~")
	}
}

func (ctrl *UsersController) UpdateEmail(c *gin.Context) {
	request := requests.UserUpdateEmailRequest{}
	if ok := requests.Validate(c, &request, requests.UserUpdateEmailSave); !ok {
		return
	}
	currentUser := auth.CurrentUser(c)
	currentUser.Email = request.Email
	rows := currentUser.Save()
	if rows > 0 {
		response.Data(c, currentUser)
	} else {
		response.Abort500(c, "请稍后尝试~")
	}
}

func (ctrl *UsersController) UpdatePhone(c *gin.Context) {
	request := requests.UserUpdatePhoneRequest{}
	if ok := requests.Validate(c, &request, requests.UserUpdatePhoneSave); !ok {
		return
	}
	currentUser := auth.CurrentUser(c)
	currentUser.Phone = request.Phone
	rows := currentUser.Save()
	if rows > 0 {
		response.Data(c, currentUser)
	} else {
		response.Abort500(c, "请稍后尝试~")
	}
}

func (ctrl *UsersController) UpdatePassword(c *gin.Context) {
	request := requests.UserUpdatePasswordRequest{}
	if ok := requests.Validate(c, &request, requests.UserUpdatePasswordSave); !ok {
		return
	}
	currentUser := auth.CurrentUser(c)
	_, err := auth.Attempt(currentUser.Email, request.PassWord)
	if err != nil {
		response.Abort500(c, "密码错误~")
	} else {
		currentUser.PassWord = request.NewPassWord
		currentUser.Save()
		response.SUCCESS(c)
	}
}

func (ctrl *UsersController) UpdateAvatar(c *gin.Context) {
	request := requests.UserUpdateAvatarRequest{}
	if ok := requests.Validate(c, &request, requests.UserUpdateAvatarSave); !ok {
		return
	}
	avatar, err := file.SaveUploadAvatar(c, request.Avatar)
	if err != nil {
		response.Abort500(c, "上传头像失败，请稍后尝试~")
		return
	}
	currentUser := auth.CurrentUser(c)
	currentUser.Avatar = config.Get("app.url") + "/" + avatar
	currentUser.Save()
	response.Data(c, currentUser)

}
